package com.qifan.energy.widget.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.qifan.energy.mvp.callback.ISuccessCallBack;
import com.qifan.energy.R;
import com.qifan.energy.bean.result.GoodsDetailResult;

public class PaySuccessDialog extends DialogFragment {

    private TextView timecardTv;
    private TextView gamefenTv;
    private TextView couponCardTv;

    private ISuccessCallBack callback = new ISuccessCallBack() {

        @Override
        public void onSuccess() {

        }
    };

    /* 显示 */
    public PaySuccessDialog showNow2(@Nullable FragmentManager manager, @Nullable String tag) {
        showNow(manager, tag);
        return this;
    }

    /* 对外提供 */
    public PaySuccessDialog setEventCallback(ISuccessCallBack callback) {
        this.callback = callback;
        return this;
    }

    /* 对外提供 */
    public PaySuccessDialog setGoodsDetailResult(GoodsDetailResult detailResult){
        if (timecardTv != null && detailResult.getProduct() != null) {
            timecardTv.setText(String.format("时间卡：%.02f分钟",detailResult.getProduct().getGiftTimeCard()));
            gamefenTv.setText(String.format("游戏分：%d分",detailResult.getProduct().getGiftGamePoint()));
            couponCardTv.setText(String.format("优惠券：%.02f元",detailResult.getProduct().getGiftCouponAmount()));
        }
        return this;
    }

    /**
     * 设置主题需要在 onCreate() 方法中调用 setStyle() 方法
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);*/
        setStyle(DialogFragment.STYLE_NO_FRAME,R.style.NormalDialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        View view = inflater.inflate(R.layout.dialog_pay_success, container);
        findView(view);
        return view;
    }

    /*@NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // 设置主题的构造方法
        // AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_pay_success, null);
        builder.setView(view);
        findView(view);
        return builder.create();
    }*/

    /* 设置View */
    private void findView(View view) {
        timecardTv = view.findViewById(R.id.timecard_tv);
        gamefenTv = view.findViewById(R.id.gamefen_tv);
        couponCardTv = view.findViewById(R.id.coupon_card_tv);
        TextView confirmTv = view.findViewById(R.id.confirm_tv);
        confirmTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onSuccess();
        });
    }
}
